package util

import (
	consulapi "github.com/hashicorp/consul/api"
	"go.uber.org/zap"
	//"errors"
)

type ConsulAPI struct {
	ConsulClient *consulapi.Client
}

func (this *ConsulAPI) RegServer() {
	config := consulapi.DefaultConfig()
	config.Address = "http://consul.weizhimubiao.cn"

	reg := consulapi.AgentServiceRegistration{}
	reg.ID = "userservice"
	reg.Name = "userservice"
	reg.Address = "http://www.weizhimubiao.cn"
	reg.Port = 80
	reg.Tags = []string{"primary"}
	check := consulapi.AgentServiceCheck{}
	check.Interval = "5s"
	check.HTTP = "http://www.weizhimubiao.cn/health"
	reg.Check = &check
	//err := new errors.Error()
	tmpConsulClient, err := consulapi.NewClient(config)
	logger, _ := zap.NewProduction()

	if err != nil {
		//panic(err)
		logger.Warn(err.Error())
	}
	this.ConsulClient = tmpConsulClient

	err = this.ConsulClient.Agent().ServiceRegister(&reg)
	if err != nil {
		logger.Warn(err.Error())
	}

}

func (this *ConsulAPI) Unregservice() {
	this.ConsulClient.Agent().ServiceDeregister("userservice")
}
